<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>proxy 拦截处理</title>
</head>
<body>

  <script>
    var person = {
      name: "张三"
    };

    // 1. 当读取不存在的属性时候，抛出错误而不是返回undefined
    var proxy = new Proxy(person, {
      // target 是目标参数 property 是调用传过来的参数
      get: function(target, property) {
        if (property in target) {
          return target[property];
        } else {
          throw new ReferenceError("Property \"" + property + "\" does not exist.");
        }
      }
    });

    console.log(proxy.name) // 张三
    // console.log(proxy.age)  // 报错 Property "age" does not exist.

    // 2. 拦截读取继承属性
    let proto = new Proxy({}, {
      get(target, propertyKey, receiver) {
        console.log('GET '+propertyKey);  //GET a
        return target[propertyKey];
      }
    });

    

    let obj = Object.create(proto);
    console.log(obj.a) // "GET xxx"
  </script>
  
</body>
</html>